﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpaceInvaders.Ships
{
    // Base Ship class
    class Ship
    {
        public Ship()
        {
        }
        // Create ship
        public void CreateShip(String Mode, ContentManager content, int startX, int startY, Color tint)
        {
        }

        // Draw ship
        public virtual void Draw(SpriteBatch spritebatch)
        {
        }

        // Shoot
        public virtual void Shoot()
        {
        }

        // Move Ship
        public virtual bool Move(GraphicsDevice GraphicsDevice,String Direction)
        {
            return false;
        }
    }

    // Player Ship Class
    class PlayerShip : Ship
    {
        // Create ship properties
        int shipWidth = 32;
        int shipHeight = 32;
        int Padding = 8;
        int MovementSpeed = 5; // Measured in Pixels
        String Movement = "Horizontal";
        Rectangle Location;
        int ShotType;
        int ShotRate;
        bool Alive = true;
        int MaxHealth;
        int CurrentHealth;
        String Mode;
        Texture2D spriteSheet;
        String DrawDirection = "Up";
        // Keyboard assignments
        Keys Right;
        Keys Left;
        Keys Up;
        Keys Down;
        Color playerColor = Color.Gray;
        Rectangle SrcRectangle = new Rectangle(0, 0, 32, 32);

        // Create ship
        public PlayerShip(String Mode, ContentManager content, int startX, int startY, Color tint, Keys Left, Keys Right, Keys Up, Keys Down)
        {
            // content.RootDirectory = "SpaceInvadersContent";
            this.spriteSheet = content.Load<Texture2D>(@"Textures\Player");
            this.Location = new Rectangle(startX, startY, shipWidth, shipHeight);
            this.playerColor = tint;
            this.MaxHealth = 100;
            this.CurrentHealth = 100;
            this.ShotType = 1;
            this.ShotRate = 25;
            this.Right = Right;
            this.Left = Left;
            this.Up = Up;
            this.Down = Down;
            this.Mode = Mode;

        }

        // Draw ship
        public override void Draw(SpriteBatch spritebatch)
        {
            Rectangle temp = new Rectangle(0, 0, 32, 32);
            // spritebatch.Draw(spriteSheet, Location,new Rectangle(0,0,32,32), playerColor,"90", new Vector2((Location.X+16),(Location.Y+16)), SpriteEffects.None, 1);
            float baseturn = 1.5655f;
            float turn = 0;
            // Get correct rotation for graphic
            if (DrawDirection == "UP")
                turn = 0;
            else if (DrawDirection == "Right")
                turn = baseturn * 1;
            else if (DrawDirection == "Down")
                turn = baseturn * 2;
            else if (DrawDirection == "Left")
                turn = baseturn * 3;

            // Per-draw Rectangle and Vecotor to facilitate rotation
            Rectangle DrawnLocation = new Rectangle(
                Location.X + (Location.Width / 2),
                        Location.Y + (Location.Height / 2),
                        Location.Height,
                        Location.Width);
            Vector2 DrawnPivot = new Vector2(Location.Width / 2,
                Location.Height / 2);

            spritebatch.Draw(spriteSheet,
                    DrawnLocation,
                    null,
                    playerColor,
                    turn,
                    DrawnPivot,
                    SpriteEffects.None, 1);
        }

        // Shoot
        public override void Shoot()
        {
        }

        // Handle Ship Movement
        public override bool Move(GraphicsDevice GraphicsDevice, String Direction)
        {
            // If currently in Horizontal movement, check for position at outside edge and allow changing to Vertical
            if (Mode == "Free")
            {
                if (Movement == "Horizontal" && (this.Location.X == Padding ||
                    this.Location.X == GraphicsDevice.Viewport.Bounds.Width - (this.Location.Width + Padding)))
                {
                    if (Keyboard.GetState().IsKeyDown(Up) || Keyboard.GetState().IsKeyDown(Down))
                    {
                        Movement = "Vertical";
                    }
                }
                else if (Movement == "Vertical" && (this.Location.Y == Padding ||
                    this.Location.Y == GraphicsDevice.Viewport.Bounds.Height - (this.Location.Height + Padding)))
                {
                    if (Keyboard.GetState().IsKeyDown(Left) || Keyboard.GetState().IsKeyDown(Right))
                    {
                        Movement = "Horizontal";
                    }
                }
            }

            // handling keyboard inputs
            if (Movement == "Horizontal")
            {
                if (Keyboard.GetState().IsKeyDown(Left)) // Left key down ?
                    this.Location.X -= MovementSpeed; // move the player
                else if (Keyboard.GetState().IsKeyDown(Right)) // Right key down ?
                    this.Location.X += MovementSpeed; // move the player

                // Set the graphic rotation direction for the ship (used in the draw method)
                if (Location.Y > GraphicsDevice.Viewport.Bounds.Height / 2)
                    DrawDirection = "Up";
                else
                    DrawDirection = "Down";
            }
            else if (Movement == "Vertical")
            {
                if (Keyboard.GetState().IsKeyDown(Up)) // Left key down ?
                    this.Location.Y -= MovementSpeed; // move the player
                else if (Keyboard.GetState().IsKeyDown(Down)) // Right key down ?
                    this.Location.Y += MovementSpeed; // move the player
                // Set the graphic rotation direction for the ship (used in the draw method)
                if (Location.X > GraphicsDevice.Viewport.Bounds.Width / 2)
                    DrawDirection = "Left";
                else
                    DrawDirection = "Right";
            }

            // limit the player's horizontal movement to the screen bounds
            if (this.Location.X < (0 + Padding)) // Left bound
            {
                this.Location.X = (0 + Padding);
            }
            else if (this.Location.X + this.Location.Width + Padding > GraphicsDevice.Viewport.Bounds.Width) // Right bound
            {
                this.Location.X = GraphicsDevice.Viewport.Bounds.Width - Padding - this.Location.Width;
            }

            // Limit the player's Vertical movement to the screen bounds
            if (this.Location.Y < (0 + Padding)) // Left bound
            {
                this.Location.Y = (0 + Padding);
            }
            else if (this.Location.Y + this.Location.Width + Padding > GraphicsDevice.Viewport.Bounds.Height) // Right bound
            {
                this.Location.Y = GraphicsDevice.Viewport.Bounds.Height - Padding - this.Location.Height;
            }
            return false;
        }
    }


    // Enemy1 Ship Class
    class EnemyShip1 : Ship
    {
        // Create ship properties
        int shipWidth = 32;
        int shipHeight = 32;
        int Padding = 16;
        int MovementSpeed = 2;
        Rectangle Location;
        int ShotType;
        int ShotRate;
        bool Alive = true;
        String Direction = "Right";
        int MaxHealth;
        int CurrentHealth;
        Texture2D spriteSheet;
        Color playerColor = Color.Gray;
        Rectangle SrcRectangle = new Rectangle(0, 0, 32, 32);
        // Create ship
        public EnemyShip1(ContentManager content, int startX, int startY, Color tint)
        {
            // content.RootDirectory = "SpaceInvadersContent";
            this.spriteSheet = content.Load<Texture2D>(@"Textures\Enemy");
            this.Location = new Rectangle(startX, startY, shipWidth, shipHeight);
            this.playerColor = tint;
            this.MaxHealth = 100;
            this.CurrentHealth = 100;
            this.ShotType = 1;
            this.ShotRate = 25;
        }

        // Draw ship
        public override void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(spriteSheet, Location, playerColor);
        }

        // Shoot
        public override void Shoot()
        {
        }

        // Move Ship
        public override bool Move(GraphicsDevice GraphicsDevice,String Direction)
        {
            // Move ship from side to side
            if (Direction == "Right")
            {
                this.Location.X += MovementSpeed;
            }
            else if (Direction == "Left")
            {
                this.Location.X -= MovementSpeed;
            }
            
            bool ChangeDirection = false;
            // Bounce box from screen edge to screen edge with a 64px padding (will need to check game state to determine padding level)
            if (Direction == "Left")
            {
                if ((this.Location.X - MovementSpeed) < Padding) 
                    ChangeDirection = true;
            }
            else if (Direction == "Right")
            {
                if ((this.Location.X + this.Location.Width + MovementSpeed) > (GraphicsDevice.Viewport.Bounds.Width - Padding))
                    ChangeDirection = true;
            }
            return ChangeDirection;
        }
    }
}
